package com.codingfuture.erp_system.service.impl;

import com.codingfuture.erp_system.dto.InventoryDto;
import com.codingfuture.erp_system.mapper.InventoryMapper;
import com.codingfuture.erp_system.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public List<InventoryDto> findGood() {
        return inventoryMapper.findGood();
    }

    @Override
    public List<InventoryDto> findStore() {
        return inventoryMapper.findStore();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(InventoryDto inventoryDto) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(new Date());
        inventoryDto.setCreatetime(format);
        Integer num = inventoryDto.getNum();
        String str = String.valueOf(num);
        Pattern pattern = Pattern.compile("^\\+?[1-9][0-9]*$");
        boolean isNum = pattern.matcher(str).matches();
        if (isNum) {
            boolean rows = inventoryMapper.insert(inventoryDto);
            if (rows) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public List<InventoryDto> findByAll(InventoryDto inventoryDto) {
        return inventoryMapper.findByAll(inventoryDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delete(Integer id) {
        return inventoryMapper.delete(id) != 0;
    }

    @Override
    public InventoryDto findById(Integer id) {
        return inventoryMapper.findById(id);
    }

    //    审核
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean update(InventoryDto inventoryDto) {


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(new Date());
        inventoryDto.setChecktime(format);
        Integer num = inventoryDto.getNum();
        String str = String.valueOf(num);
        Pattern pattern = Pattern.compile("^\\+?[1-9][0-9]*$");
        boolean isNum = pattern.matcher(str).matches();
        if (isNum) {
            boolean rows = inventoryMapper.update(inventoryDto);
            if (rows) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

}
